<?php

class Application_Model_jqxGrid
{
	private $properties;
	private $source;
	
	public  function __construct()
	{
		
	}
	
	public function lista($valores)
	{
		$properties = $this->allGridProperties();
		$lista = "//Grid
            var url = '$valores[url]';
            
            // prepare the data
            var source =
            {
                datatype: 'json',
                datafields: [
                	{ name: 'id_clientes' },
                    { name: 'nombre' },
                    { name: 'apellidos' },
                    { name: 'nif'},
                    { name: 'pais'},
                    { name: 'cod_tipo_clie'},
                ],
                id: 'id',
                url: url,
                root: 'data'
            };
            var dataAdapter = new $.jqx.dataAdapter(source);

            $('$valores[nombreGrid]').jqxGrid(
            {
            	$properties            	
                scrollbarsize: 15,
                source: dataAdapter,
                theme: theme,
                columns: [
                  { text: 'ID Cliente', dataField: 'id_clientes', width: 80, cellsalign: 'right' },
                  { text: 'Nombre', dataField: 'nombre', minwidth: 100 },
                  { text: 'Apellidos', dataField: 'apellidos', minwidth: 100 },
                  { text: 'NIF / Pasaporte', dataField: 'nif', width: 180 },
                  { text: 'Pa&iacute;s', dataField: 'pais', width: 80, cellsalign: 'right' },
                  { text: 'Tipo', dataField: 'cod_tipo_clie', width: 90, cellsalign: 'right', cellsformat: 'c2' },                  
                ]
            });";
		return $lista;
	}
	
	public function dbclickForm($pk, $campo2, $nombreGrid, $url )
	{
		$evento = "
			$('$nombreGrid').bind('rowdoubleclick', function (event) 
			{
				id = valorGridIdUnico(this, '$pk');
			    muestraVentanaHTML(700, 520, id, valorGridIdUnico(this, '$campo2'));
		     	$.ajax({
				    data: '',
				    type: 'GET',
				    dataType: 'html',
				    url: '$url'+id,
				    success: function(resultado){ 
				    	$('#jqxWindowContent').html(resultado);
   					}
		   		});

			    //window.console.log(valorGrid(this, 'id_clientes'));
			});";
		return $evento;
	}
	
	public function addGridProperty($nombre, $valor)
	{
		$this->properties[$nombre] = $valor;
	}
	private function allGridProperties()
	{
		$properties ='';
		foreach ($this->properties as $key =>$value) {
			$properties .= "$key: $value,\n\t\t\t\t";
		}
		return $properties;
	}
}

